/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * BancoTable.java
 *
 * Created on 06/08/2010, 09:28:09
 */

package com.power.powercash;

import com.db4o.ObjectContainer;
import java.util.LinkedList;
import java.awt.Dimension;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.RowSorter;
import javax.swing.table.TableRowSorter;

/**
 *
 * @author f6036477
 */
public class BancoTable extends javax.swing.JPanel {

  private BancoTableModel model;

    /** Creates new form BancoTable */
    public BancoTable(List<Banco> list) {
      if(list == null)
        list = new LinkedList<Banco>();
      model = new BancoTableModel(list);
      initComponents();

      RowSorter<BancoTableModel> sorter =
          new TableRowSorter<BancoTableModel>(model);
      tabela.setRowSorter(sorter);
    }

    public Banco getSelected()
    {
      int r = tabela.getSelectedRow();
      if(r < 0 || r > model.getRowCount())
        return null;

      return model.getObject(r);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    scroll = new javax.swing.JScrollPane();
    tabela = new javax.swing.JTable();

    setBorder(javax.swing.BorderFactory.createEtchedBorder());

    Dimension dm = new Dimension(300, 300);
    scroll.setSize(dm);
    scroll.setPreferredSize(dm);

    tabela.setModel(model);
    scroll.setViewportView(tabela);

    add(scroll);
  }// </editor-fold>//GEN-END:initComponents


  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JScrollPane scroll;
  private javax.swing.JTable tabela;
  // End of variables declaration//GEN-END:variables


  public static void main(String[] args)
  {
    JFrame f = new JFrame("BancoTable");
    f.setLocation(300, 150);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Configuration conf = new Configuration();
    conf.setDBFile("bancos.db");
    ObjectContainer db = conf.startDB();

    Banco bb =  new Banco();
    Banco brad = new Banco();
    Banco itau = new Banco();
    bb.setName("BB");
    bb.setAgencia(1);
    brad.setName("Bradesco");
    brad.setAgencia(2);
    itau.setName("Itau");
    itau.setAgencia(3);

    for(int i = 1; i < 9; i++) {
      bb = bb.clone();
      bb.setConta(i*10);
      db.store(bb);

      brad = brad.clone();
      brad.setConta(i*15);
      db.store(brad);

      itau = itau.clone();
      itau.setConta(i*20);
      db.store(itau);
    }

    BancoTable bt = new BancoTable(db.query(Banco.class));

    f.add(bt);
    f.pack();

    f.setVisible(true);
  }

}
